package 队列.迷宫队列;

import java.util.LinkedList;
import java.util.Queue;

class MazeClass {
    final int MaxSize = 20;
    int[][] mg;  // 迷宫数组
    int m, n;  // 迷宫的行数和列数
    Queue<Box> qu;  // 队列

    public MazeClass(int m1, int n1) {
        m = m1;
        n = n1;
        mg = new int[MaxSize][MaxSize];
        qu = new LinkedList<Box>();
    }

    public void Setmg(int[][] a) {
        for (int i = 0; i < m; i++)
            if (n >= 0) System.arraycopy(a[i], 0, mg[i], 0, n);
    }

    public void Dispatch(Box p) {
        int cnt = 1;
        while (p != null) {
            System.out.print("[" + p.i + "," + p.j + "]");
            if (cnt % 5 == 0)
                System.out.println();
            cnt++;
            p = p.pre;
        }
        System.out.println();
    }

    boolean mgpath(int xi, int yi, int xe, int ye) {
        int i = xi, j = yi;
        Box b, b1;
        b = new Box(xi, yi);
        qu.offer(b);
        mg[xi][yi] = -1;

        while (!qu.isEmpty()) {
            b = qu.poll();
            if (b.i == xe && b.j == ye) {
                Dispatch(b);
                return true;
            }
            for (int di = 0; di < 4; di++) {
                switch (di) {
                    case 0: i = b.i - 1; j = b.j; break;
                    case 1: i = b.i; j = b.j + 1; break;
                    case 2: i = b.i + 1; j = b.j; break;
                    case 3: i = b.i; j = b.j - 1; break;
                }
                if (mg[i][j] == 0) {
                    b1 = new Box(i, j);
                    b1.pre = b;
                    qu.offer(b1);
                    mg[i][j] = -1;
                }
            }
        }
        return false;
    }
}